home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / DCLAP 6d / dclap6d / network / apple.old / dnr.c < prev    next >
Text File  |  1996-07-05  |  6KB  |  223 lines

  1. /*     DNR.c - DNR library for MPW
  2.  
  3.     (c) Copyright 1988 by Apple Computer.  All rights reserved
  4.     
  5. */
  6.  
  7. #define MPW3 true
  8.  
  9. #include <Types.h>
  10. #include <OSUtils.h>
  11. #include <Errors.h>
  12. #include <Files.h>
  13. #include <Resources.h>
  14. #ifdef MPW3 
  15. #include <Memory.h>
  16. #endif
  17.  
  18. /* #include <AddressXlation.h>*/
  19. typedef pascal void (*EnumResultProcPtr)(struct cacheEntryRecord *cacheEntryRecordPtr, char *userDataPtr);
  20.  
  21. typedef pascal void (*ResultProcPtr)(struct hostInfo *hostInfoPtr, char *userDataPtr);
  22.  
  23.  
  24. #define OPENRESOLVER    1
  25. #define CLOSERESOLVER    2
  26. #define STRTOADDR        3
  27. #define    ADDRTOSTR        4
  28. #define    ENUMCACHE        5
  29. #define ADDRTONAME        6
  30.  
  31. Handle codeHndl = NULL;
  32.  
  33. typedef OSErr (*OSErrProcPtr)();
  34. OSErrProcPtr dnr = NULL;
  35.  
  36. typedef struct hostInfo  *hostInfoPtr; /* dgg for codewar */
  37.  
  38. // jack for PPC calls -- dgg
  39.  
  40. enum {
  41.     uppOPENRESOLVERProcInfo = kCStackBased
  42.          | RESULT_SIZE(SIZE_CODE(sizeof(OSErr)))
  43.          | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(long)))
  44.          | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(char*)))
  45. };
  46.  
  47. enum {
  48.     uppCLOSERESOLVERProcInfo = kCStackBased
  49.          | RESULT_SIZE(SIZE_CODE(sizeof(OSErr)))
  50.          | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(long)))
  51. };
  52.  
  53. enum {
  54.     uppSTRTOADDRProcInfo = kCStackBased
  55.          | RESULT_SIZE(SIZE_CODE(sizeof(OSErr)))
  56.          | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(long)))
  57.          | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(char*)))
  58.          | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof(hostInfoPtr)))
  59.          | STACK_ROUTINE_PARAMETER(4, SIZE_CODE(sizeof(ResultProcPtr)))
  60.          | STACK_ROUTINE_PARAMETER(5, SIZE_CODE(sizeof(char*)))
  61. };
  62.  
  63.  
  64. typedef OSErr (*CLOSERESOLVERProcPtr)(short);
  65.  
  66. typedef OSErr (*ADDRTONAMEProcPtr)( short,  
  67.     unsigned long addr, hostInfoPtr rtnStruct, 
  68.     ResultProcPtr resultproc, char *userDataPtr);
  69.  
  70. typedef OSErr (*ENUMCACHEProcPtr)( short,  
  71.     EnumResultProcPtr resultproc, char *userDataPtr);
  72.  
  73. typedef OSErr (*ADDRTOSTRProcPtr)( short,  
  74.     unsigned long addr,char * addrStr);
  75.     
  76. typedef OSErr (*STRTOADDRProcPtr)( short,  
  77.     char *hostName, hostInfoPtr rtnStruct, ResultProcPtr resultproc,char * userDataPtr);
  78.  
  79. typedef OSErr (*OPENRESOLVERProcPtr)( short,  
  80.     char *fileName);
  81.  
  82.  
  83. /* OpenOurRF is called to open the MacTCP driver resources */
  84.  
  85. short OpenOurRF() {
  86.     SysEnvRec info;
  87.     HParamBlockRec fi;
  88.     Str255 filename;
  89.     
  90.     SysEnvirons(1, &info);
  91.     
  92.     fi.fileParam.ioCompletion    = NULL;
  93.     fi.fileParam.ioNamePtr        = (StringPtr)&filename;
  94.     fi.fileParam.ioVRefNum        = info.sysVRefNum;
  95.     fi.fileParam.ioDirID        = 0;
  96.     fi.fileParam.ioFDirIndex    = 1;
  97.     
  98.     while (PBHGetFInfo(&fi, false) == noErr) {
  99.         /* scan system folder for driver resource files of specific type & creator */
  100.         if (fi.fileParam.ioFlFndrInfo.fdType == 'cdev' && fi.fileParam.ioFlFndrInfo.fdCreator == 'mtcp') {
  101.             /* found the MacTCP driver file */
  102.             SetVol(0,info.sysVRefNum);
  103.             return(OpenResFile( (const unsigned char *)&filename));
  104.         }
  105.         
  106.         /* check next file in system folder */
  107.         fi.fileParam.ioFDirIndex++;
  108.         fi.fileParam.ioDirID = 0;
  109.     }
  110.     return(-1);
  111. }
  112.  
  113.  
  114.  
  115. OSErr OpenResolver(char *fileName)
  116. {
  117.     short refnum;
  118.     OSErr rc;
  119.  
  120.     if (dnr != NULL)
  121.         /* resolver already loaded in */
  122.         return(noErr);
  123.         
  124.     /* open the MacTCP driver to get DNR resources. Search for it based on
  125.        creator & type rather than simply file name */    
  126.     refnum = OpenOurRF();
  127.  
  128.     /* ignore failures since the resource may have been installed in the 
  129.        System file if running on a Mac 512Ke */
  130.        
  131.     /* load in the DNR resource package */
  132.     codeHndl = GetIndResource('dnrp', 1);
  133.     if (codeHndl == NULL) {
  134.         /* can't open DNR */
  135.         return(ResError());
  136.         }
  137.     
  138.     DetachResource(codeHndl);
  139.     if (refnum != -1) {
  140.         CloseWD(refnum); /* dgg */
  141.         CloseResFile(refnum);
  142.         }
  143.         
  144.     /* lock the DNR resource since it cannot be relocated while opened */
  145.     MoveHHi(codeHndl);// dgg -- dnr handle will be open/locked long time...
  146.     HLock(codeHndl);
  147.     dnr = (OSErrProcPtr) *codeHndl;
  148.  
  149.     /* call open resolver */
  150. #if USESROUTINEDESCRIPTORS
  151. //#if defined(powerc) || defined (__powerc)
  152.     rc = CallUniversalProc((UniversalProcPtr)(dnr),
  153.          uppOPENRESOLVERProcInfo, OPENRESOLVER, fileName);
  154. #else
  155.     rc = (*dnr)(OPENRESOLVER, fileName);
  156. #endif
  157.  
  158.     if (rc != noErr) {
  159.         /* problem with open resolver, flush it */
  160.         HUnlock(codeHndl);
  161.         DisposHandle(codeHndl);
  162.         dnr = NULL;
  163.         }
  164.  
  165.     return(rc);
  166. }
  167.  
  168.  
  169. OSErr CloseResolver()
  170. {
  171.     if (dnr == NULL) return(notOpenErr);
  172.         
  173.     /* call close resolver */
  174. #if USESROUTINEDESCRIPTORS
  175.     (void) CallUniversalProc((UniversalProcPtr)(dnr),
  176.          uppCLOSERESOLVERProcInfo, CLOSERESOLVER);
  177. #else
  178.     (void) (*dnr)(CLOSERESOLVER);
  179. #endif
  180.  
  181.     /* release the DNR resource package */
  182.     HUnlock(codeHndl);
  183.     DisposHandle(codeHndl);
  184.     dnr = NULL;
  185.     return(noErr);
  186. }
  187.  
  188. OSErr StrToAddr(char *hostName, hostInfoPtr rtnStruct, ResultProcPtr resultproc,char * userDataPtr)
  189. {
  190.     if (dnr == NULL) return(notOpenErr);
  191.     
  192. #if USESROUTINEDESCRIPTORS
  193.     return( CallUniversalProc((UniversalProcPtr)(dnr),
  194.          uppSTRTOADDRProcInfo, STRTOADDR, hostName, rtnStruct, resultproc, userDataPtr));
  195. #else
  196.     return((*dnr)(STRTOADDR, hostName, rtnStruct, resultproc, userDataPtr));
  197. #endif
  198. }
  199.     
  200. OSErr AddrToStr(unsigned long addr,char * addrStr)
  201. {
  202.     if (dnr == NULL) return(notOpenErr);
  203.  
  204.     (*dnr)(ADDRTOSTR, addr, addrStr);
  205.     return(noErr);
  206. }
  207.     
  208. OSErr EnumCache(EnumResultProcPtr resultproc, char *userDataPtr)
  209. {
  210.     if (dnr == NULL) return(notOpenErr);
  211.         
  212.     return((*dnr)(ENUMCACHE, resultproc, userDataPtr));
  213. }
  214.     
  215.     
  216. OSErr AddrToName(unsigned long addr, hostInfoPtr rtnStruct, 
  217.     ResultProcPtr resultproc, char *userDataPtr)
  218. {
  219.     if (dnr == NULL) return(notOpenErr);
  220.         
  221.     return((*dnr)(ADDRTONAME, addr, rtnStruct, resultproc, userDataPtr));
  222. }
  223.